package com.ruoyi.records.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysDetailed;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.detailed.mapper.DetailedMapper;
import com.ruoyi.prize.domain.Prize;
import com.ruoyi.prize.mapper.PrizeMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;
import com.ruoyi.records.mapper.ExchangeRecordsMapper;
import com.ruoyi.records.domain.ExchangeRecords;
import com.ruoyi.records.service.IExchangeRecordsService;

/**
 * 兑换记录Service业务层处理
 * 
 * @author 秦永昆
 * @date 2023-02-12
 */
@Service
public class ExchangeRecordsServiceImpl implements IExchangeRecordsService 
{
    @Autowired
    private ExchangeRecordsMapper exchangeRecordsMapper;

    @Autowired
    private DetailedMapper detailedMapper;

    @Autowired
    private PrizeMapper prizeMapper;

    @Autowired SysUserMapper sysUserMapper;

    @Autowired
    private SysUserMapper userMapper;
    /**
     * 查询兑换记录
     * 
     * @param exchangeRecordsId 兑换记录主键
     * @return 兑换记录
     */
    @Override
    public ExchangeRecords selectExchangeRecordsByExchangeRecordsId(Long exchangeRecordsId)
    {
        return exchangeRecordsMapper.selectExchangeRecordsByExchangeRecordsId(exchangeRecordsId);
    }

    /**
     * 查询兑换记录列表
     * 
     * @param exchangeRecords 兑换记录
     * @return 兑换记录
     */
    @Override
    public List<ExchangeRecords> selectExchangeRecordsList(ExchangeRecords exchangeRecords)
    {
        return exchangeRecordsMapper.selectExchangeRecordsList(exchangeRecords);
    }

    /**
     * 查询我的兑换记录列表
     *
     * @param exchangeRecords 兑换记录
     * @return 兑换记录
     */
    @Override
    public List<ExchangeRecords> selectMyExchangeRecordsList(ExchangeRecords exchangeRecords)
    {
        exchangeRecords.setCreateBy(SecurityUtils.getUsername());
        return exchangeRecordsMapper.selectMyExchangeRecordsList(exchangeRecords);
    }


    /**
     * 新增兑换记录
     * 
     * @param exchangeRecords 兑换记录
     * @return 结果
     */
    @Override
    public int insertExchangeRecords(ExchangeRecords exchangeRecords)
    {
        exchangeRecords.setCreateTime(DateUtils.getNowDate());
        exchangeRecords.setCreateBy(SecurityUtils.getUsername());

        SysUser sysUser = sysUserMapper.selectUserById(SecurityUtils.getUserId());
        if (sysUser==null){
            throw new ServiceException("用户信息错误,请联系管理员");
        }
        Prize prize = prizeMapper.selectPrizeByPrizeId(exchangeRecords.getPrizeId());
        if (prize==null||prize.getPrizeQuantity()<=prize.getPrizeOutQuantity()){
            throw new ServiceException("兑换商品不存在");
        }
        if (sysUser.getIntegral()<prize.getPrizeIntegral()){
            throw new ServiceException("积分不足");
        }

        SysDetailed detailed=new SysDetailed();
        detailed.setUserName(sysUser.getUserName());
        detailed.setPhonenumber(sysUser.getPhonenumber());
        detailed.setChangeReason("兑换:"+prize.getPrizeName());
        detailed.setChangeValue(0-prize.getPrizeIntegral());
        detailed.setCreateTime(new Date());
        userMapper.insertDetailed(detailed);


        SysUser s=new SysUser();
        s.setUserId(SecurityUtils.getUserId());
        s.setIntegral(sysUser.getIntegral()-prize.getPrizeIntegral());
        sysUserMapper.updateUser(s);

        Prize p=new Prize();

        p.setPrizeId(prize.getPrizeId());
        p.setPrizeOutQuantity(prize.getPrizeOutQuantity()+1);
        prizeMapper.updatePrize(p);

        ExchangeRecords e=new ExchangeRecords();
        e.setPrizeId(exchangeRecords.getPrizeId());
        e.setCreateBy(SecurityUtils.getUsername());
        e.setCreateTime(new Date());



        return exchangeRecordsMapper.insertExchangeRecords(e);
    }

    /**
     * 修改兑换记录
     * 
     * @param exchangeRecords 兑换记录
     * @return 结果
     */
    @Override
    public int updateExchangeRecords(ExchangeRecords exchangeRecords)
    {
        exchangeRecords.setUpdateTime(DateUtils.getNowDate());
        return exchangeRecordsMapper.updateExchangeRecords(exchangeRecords);
    }

    /**
     * 批量删除兑换记录
     * 
     * @param exchangeRecordsIds 需要删除的兑换记录主键
     * @return 结果
     */
    @Override
    public int deleteExchangeRecordsByExchangeRecordsIds(Long[] exchangeRecordsIds)
    {
        return exchangeRecordsMapper.deleteExchangeRecordsByExchangeRecordsIds(exchangeRecordsIds);
    }

    /**
     * 删除兑换记录信息
     * 
     * @param exchangeRecordsId 兑换记录主键
     * @return 结果
     */
    @Override
    public int deleteExchangeRecordsByExchangeRecordsId(Long exchangeRecordsId)
    {
        return exchangeRecordsMapper.deleteExchangeRecordsByExchangeRecordsId(exchangeRecordsId);
    }
}
